home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / Compression / bitwiseclass.pas next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  7.3 KB  |  292 lines

  1. unit bitwiseclass;
  2.  
  3. {*=====================================================================
  4.   Classes:   ByteBitWise, WordBitWise, DWordBitWise, DDWordBitWise and
  5.              BitWise
  6.  
  7.   File:      bitwiseclass.pas
  8.  
  9.   Summary:
  10.  
  11.        ByteBitWise, WordBitWise, DWordBitWise, DDWordBitWise  are
  12.        classes to support shl and shr operations at byte, word (UInt16),
  13.        DWORD (UInt32) or UInt64 (DDWord) level need unsafe .NET by default.
  14.        The 5th class is BitWise, which useable for these level by
  15.        demonstrate the selection of the best method according to length of
  16.        parameter. This class also contain methods to select HI and LO bytes
  17.        from a Word or HI and LO Words of a DWord respectively. So, you can
  18.        reach all of the bytes of a DWord.
  19.  
  20.        //UInt64 bit methods probably need more tests
  21. ---------------------------------------------------------------------
  22.   This file is submitted by:
  23.  
  24.   endresy@axelero.hu
  25.   Endre I. Simay,
  26.   Hungary
  27.  
  28. THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  29. KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  30. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  31. PARTICULAR PURPOSE.
  32. =====================================================================*}
  33. interface
  34.  
  35. type
  36.   ByteBitWise = class
  37.     ext: array[0..7] of byte;
  38.   public
  39.     constructor Create;
  40.     function byteshr(basic: byte; n: byte): byte;
  41.     function byteshl(basic: byte; n: byte): byte;
  42.   end;
  43.   TByteBitWise = class (ByteBitWise);
  44.  
  45.   WordBitWise = class
  46.     ext: array[0..15] of word;
  47.   public
  48.     constructor Create;
  49.     function wordshr(basic: word; n: word): word;
  50.     function wordshl(basic: word; n: word): word;
  51.   end;
  52.   TWordBitWise = class (WordBitWise);
  53.  
  54.   DWordBitWise = class
  55.     ext: array[0..31] of UInt32;
  56.   public
  57.     constructor Create;
  58.     function dwordshr(basic: UInt32; n: UInt32): UInt32;
  59.     function dwordshl(basic: UInt32; n: UInt32): UInt32;
  60.   end;
  61.   TDWordBitWise = class (DWordBitWise);
  62.  
  63.   DDWordBitWise = class
  64.     ext: array[0..63] of UInt64;
  65.   public
  66.     constructor Create;
  67.     function ddwordshr(basic: UInt64; n: UInt64): UInt64;
  68.     function ddwordshl(basic: UInt64; n: UInt64): UInt64;
  69.   end;
  70.   TDDWordBitWise = class (DDWordBitWise);
  71.  
  72.   BitWise = class
  73.     ext8: array[0..7] of byte;
  74.     ext16: array[0..15] of word;
  75.     ext32: array[0..31] of UInt32;
  76.     ext64: array[0..63] of UInt64;
  77.   public
  78.     constructor Create;
  79.     function bitwiseshr(basic: byte; n: byte): byte; overload;
  80.     function bitwiseshl(basic: byte; n: byte): byte; overload;
  81.  
  82.     function bitwiseshr(basic: word; n: word): word; overload;
  83.     function bitwiseshl(basic: word; n: word): word; overload;
  84.  
  85.     function bitwiseshr(basic: UInt32; n: UInt32): UInt32; overload;
  86.     function bitwiseshl(basic: UInt32; n: UInt32): UInt32; overload;
  87.  
  88.     function bitwiseshr(basic: UInt64; n: UInt64): UInt64; overload;
  89.     function bitwiseshl(basic: UInt64; n: UInt64): UInt64; overload;
  90.  
  91.     function bitwiseLO(basic: word): byte; overload;
  92.     function bitwiseHI(basic: word): byte; overload;
  93.  
  94.     function bitwiseLO(basic: UInt32): word; overload;
  95.     function bitwiseHI(basic: UInt32): word; overload;
  96.  
  97.     function bitwiseLO(basic: UInt64): UInt32; overload;
  98.     function bitwiseHI(basic: UInt64): UInt32; overload;
  99.   end;
  100.   TBitWise = class (BitWise);
  101.  
  102. implementation
  103.  
  104. constructor ByteBitWise.Create;
  105. var
  106.   i: integer;
  107. begin
  108.   inherited;
  109.   ext[0] := 1;
  110.   for i := 1 to 7 do // 1, 2, 4, 8, 16, 32, 64, 128...;
  111.   begin
  112.     ext[i] := byte(ext[i - 1] * 2);
  113.   end;
  114. end;
  115.  
  116. function ByteBitWise.byteshr(basic: byte; n: byte): byte;
  117. begin
  118.   Result := byte(basic div ext[n and (8 - 1)]);
  119. end;
  120.  
  121. function ByteBitWise.byteshl(basic: byte; n: byte): byte;
  122. begin
  123.   Result := byte(basic * ext[n and (8 - 1)]);
  124. end;
  125.  
  126.  
  127. constructor WordBitWise.Create();
  128. var
  129.   i: integer;
  130. begin
  131.   inherited;
  132.   ext[0] := 1;
  133.   for i := 1 to 15 do // 1, 2, 4, 8, 16, 32, 64, 128...;
  134.   begin
  135.     ext[i] := word(ext[i - 1] * 2);
  136.   end;
  137. end;
  138.  
  139. function WordBitWise.wordshr(basic: word; n: word): word;
  140. begin
  141.   Result := word(basic div ext[n and (16 - 1)]);
  142. end;
  143.  
  144. function WordBitWise.wordshl(basic: word; n: word): word;
  145. begin
  146.   Result := word(basic * ext[n and (16 - 1)]);
  147. end;
  148.  
  149. constructor DWordBitWise.Create();
  150. var
  151.   i: integer;
  152. begin
  153.   inherited;
  154.   ext[0] := 1;
  155.   for i := 1 to 31 do // 1, 2, 4, 8, 16, 32, 64, 128...;
  156.   begin
  157.     ext[i] := UInt32(ext[i - 1] * 2);
  158.   end;
  159. end;
  160.  
  161. function DWordBitWise.dwordshr(basic: UInt32; n: UInt32): UInt32;
  162. begin
  163.   Result := UInt32(basic div ext[n and (32 - 1)]);
  164. end;
  165.  
  166. function DWordBitWise.dwordshl(basic: UInt32; n: UInt32): UInt32;
  167. begin
  168.   Result := UInt32(basic * ext[n and (32 - 1)]);
  169. end;
  170.  
  171. constructor DDWordBitWise.Create();
  172. var
  173.   i: integer;
  174. begin
  175.   inherited;
  176.   ext[0] := 1;
  177.   for i := 1 to 31 do // 1, 2, 4, 8, 16, 32, 64, 128...;
  178.   begin
  179.     ext[i] := UInt64(ext[i - 1] * 2);
  180.   end;
  181. end;
  182.  
  183. function DDWordBitWise.ddwordshr(basic: UInt64; n: UInt64): UInt64;
  184. begin
  185.   Result := UInt64(basic div ext[n and (64 - 1)]);
  186. end;
  187.  
  188. function DDWordBitWise.ddwordshl(basic: UInt64; n: UInt64): UInt64;
  189. begin
  190.   Result := UInt64(basic * ext[n and (64 - 1)]);
  191. end;
  192.  
  193. constructor BitWise.Create();
  194. var
  195.   i: integer;
  196. begin
  197.   inherited;
  198.   ext8[0] := 1;
  199.   ext16[0] := 1;
  200.   ext32[0] := 1;
  201.   ext64[0] := 1;
  202.  
  203.   for i := 1 to 63 do // 1, 2, 4, 8, 16, 32, 64, 128...;
  204.   begin
  205.     if (i < 8) then
  206.     begin
  207.       ext8[i] := byte(ext8[i - 1] * 2);
  208.     end;
  209.     if (i < 16) then
  210.     begin
  211.       ext16[i] := word(ext16[i - 1] * 2);
  212.     end;
  213.     if (i < 32) then
  214.     begin
  215.       ext32[i] := UInt32(ext32[i - 1] * 2);
  216.     end;
  217.     ext64[i] := UInt64(ext64[i - 1] * 2);
  218.   end;
  219. end;
  220.  
  221. function BitWise.bitwiseshr(basic: byte; n: byte): byte;
  222. begin
  223.   Result := byte(basic div ext8[n and (8 - 1)]);
  224. end;
  225.  
  226. function BitWise.bitwiseshl(basic: byte; n: byte): byte;
  227. begin
  228.   Result := byte(basic * ext8[n and (8 - 1)]);
  229. end;
  230.  
  231. function BitWise.bitwiseshr(basic: word; n: word): word;
  232. begin
  233.   Result := (basic div ext16[n and (16 - 1)]);
  234. end;
  235.  
  236. function BitWise.bitwiseshl(basic: word; n: word): word;
  237. begin
  238.   Result := (basic * ext16[n and (16 - 1)]);
  239. end;
  240.  
  241. function BitWise.bitwiseshr(basic: UInt32; n: UInt32): UInt32;
  242. begin
  243.   Result := UInt32(basic div ext32[n and (32 - 1)]);
  244. end;
  245.  
  246. function BitWise.bitwiseshl(basic: UInt32; n: UInt32): UInt32;
  247. begin
  248.   Result := UInt32(basic * ext32[n and (32 - 1)]);
  249. end;
  250.  
  251. function BitWise.bitwiseshr(basic: UInt64; n: UInt64): UInt64;
  252. begin
  253.   Result := UInt64(basic div ext64[n and (64 - 1)]);
  254. end;
  255.  
  256. function BitWise.bitwiseshl(basic: UInt64; n: UInt64): UInt64;
  257. begin
  258.   Result := UInt64(basic * ext64[n and (64 - 1)]);
  259. end;
  260.  
  261. function BitWise.bitwiseLO(basic: word): byte;
  262. begin
  263.   Result := byte(basic);
  264. end;
  265.  
  266. function BitWise.bitwiseHI(basic: word): byte;
  267. begin
  268.   Result := byte(bitwiseshr((basic), word(8)));
  269. end;
  270.  
  271. function BitWise.bitwiseLO(basic: UInt32): word;
  272. begin
  273.   Result := word(basic);
  274. end;
  275.  
  276. function BitWise.bitwiseHI(basic: UInt32): word;
  277. begin
  278.   Result := word(bitwiseshr(UInt32(basic), UInt32(16)));
  279. end;
  280.  
  281. function BitWise.bitwiseLO(basic: UInt64): UInt32;
  282. begin
  283.   Result := UInt32(basic);
  284. end;
  285.  
  286. function BitWise.bitwiseHI(basic: UInt64): UInt32;
  287. begin
  288.   Result := UInt32(bitwiseshr((basic), UInt64(32)));
  289. end;
  290.  
  291. end.
  292.